import cv2

image = cv2.imread("source/4.jpg")  #加载照片
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)  #灰度处理
#cv2.imshow("Frank Bu1",image)   #显示灰度处理后的照片
face_cascade = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml')  #使用训练模型加载定位脸位置

faces = face_cascade.detectMultiScale(
   gray,
   scaleFactor = 1.1,
   minNeighbors = 5,
   minSize = (5,5)
)

print ("发现{0}个人脸!".format(len(faces)))

for(x,y,w,h) in faces:
    num1 = 0;
    if (x+x+w)%2==1:
        num1 = ((x+x+w)+1)/2
    else:
        num1 = (x+x+w)/2

    num2 = 0;
    if (y+y+h) % 2 == 1:
        num2 = ((y+y+h) + 1) / 2
    else:
        num2 = (y+y+h) / 2

    num3 = 0;
    if w % 2 == 1:
        num3 = (w + 1) / 2
    else:
        num3 = w / 2

    cv2.circle(image,(int(num1),int(num2)),int(num3),(0,255,0),2)

cv2.imshow("Frank Bu",image)
cv2.waitKey (0) #等待结束
cv2.destroyAllWindows()  #销毁窗口




